import java.util.*;
import java.io.*;


/*
	Tester class for file I/O basics.

	You will modify this class and add the following methods:

	*openInFile: Connects either a Scanner or BufferedReader object to the
	specified input file.  The method should handle any exceptions that may
	occur when opening the specified file and return a Boolean value that shows
	whether or not the operation succeeded.

	*openOutFile: Connects a PrintWriter object to the specified file.  The
	method should handle any exception that may occur, write an error message
	specifying what happened, and then exit the program.

	*closeInFile: Disconnects Scanner or BufferedReader from file on disk, provided
	a connection exists.

	*closeOutFile: Disconnects PrintWriter from file on disk, provided
	a connection exists.

	*readShape: Will read data on a single shape from the input file object, provided
	there is a Scanner/BufferedReader object set up.

	Note that the input file may be	empty.

	If there is data in the input file, a single entry	will consist of the shape name
	(Circle or Square) on a line of its own, followed by the radius/length on a line
	of its own.  All lines of data with end with a newline.

	After reading the shape information, the method should create and
	return the appropriate shape object.

	If there is no data to read, the method should return null.

	HELPFUL HINT:
	-->Check the input files in the Part6 folder to get an idea of what
	   you are dealing with in terms of input.

	*writeToOutFile: Will write the data passed in to the output file object
	followed by a newline -- provided there is a PrintWriter object set up.




*/
public class Part6Tester
{
	private static Scanner inFileA;
	private static BufferedReader inFileB;
	private static PrintWriter outFile;


	public static void main(String [] args)
	{
		//helper variables for testing
		ArrayList<Shape> list = new ArrayList<Shape>();
		Shape shape;
		boolean success;

		String [] inFiles = {"shapes1.txt", "shapes2input.txt", "i_dont_exist.txt"};


		//process each input file name
		for (int i = 0; i < inFiles.length; i++)
		{
			success = openInFile(inFiles[i]);

			if (success) //file opened successfully
			{

				shape = readShape();

				//echo shape read to console
				System.out.println("Just *read*: \n" + shape);
				while (shape != null)
				{
					list.add(shape);
					shape = readShape();

					//echo shape read to console
					System.out.println("Just *read*: \n" + shape);

				}//end while

				//create output file named based on input file
				openOutFile(inFiles[i] + ".out");

				System.out.println();

				//write list contents to output file
				for (int j = 0; j < list.size(); j++)
				{
					writeToOutFile( list.get(j) );

					//echo shape that was just written to output file to console
					System.out.println("Just WROTE: \n" + list.get(j));
				}

				closeOutFile();

			}
			else
				System.out.println("\n***Failed to open input file: " + inFiles[i] + "***");

			closeInFile();
		}

		System.out.println("\nDone testing Part6...");



	}//end main method

//*********************************************************************
// Write your methods below

	//choose either a Scanner or BufferedReader for your input file object
	private static boolean openInFile(String fileName)
	{
		boolean success = false;
/*ans bl*/
		try
		{
			inFileA = new Scanner( new File(fileName) );
			success = true;

		}
		catch(Exception e)
		{


		}
/*ans ab*/

		return success;

	}


	//read from input file object that is open
	private static Shape readShape()
	{
		Shape s = null;
/*ans bl*/
		if (inFileA != null)
		{
			if (inFileA.hasNext())
			{
				String name;
				double length;

				name = inFileA.nextLine();
				length = inFileA.nextDouble();
				inFileA.nextLine(); //get rid of extra \n

				if (name.equals("Circle"))
					return new Circle(length);
				else
					return new Square(length);

			}

		}
/*ans ab*/


		return s;

	}

	private static void openOutFile(String fileName)
	{
/*ans bl*/
		try
		{

			outFile = new PrintWriter(new File(fileName));

		}
		catch (Exception e)
		{
			System.out.println("Output file named: " + fileName + " failed to open");
			System.out.println("EXITING");
			System.exit(-1);
		}
/*ans ab*/


	}

	//only close file if there is something to close
	private static void closeInFile()
	{
/*ans bl*/

		if (inFileA != null)
			inFileA.close();

/*ans ab*/
	}

	//only close file if there is something to close
	private static void closeOutFile()
	{
/*ans bl*/
		if (outFile != null)
			outFile.close();
/*ans ab*/



	}

	//only write to file if there is a file to write to
	private static void writeToOutFile(Object o)
	{
/*ans bl*/
		if (outFile != null)
			outFile.println(o);
/*ans ab*/



	}

}//end class Part6Tester
